package org.kjtc.service;

import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.kjtc.redis.RedisUtil;
import org.kjtc.weChatUtil.JsSignUtil;
import org.kjtc.weChatUtil.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.Map;

/**
 * Created by xiaojinlu1990@163.com on 2019/3/8  .
 */
@Service
public class WxMpService {
    /**
     *  获取token并缓存到Redis
     * @return
     */
    @Autowired
    private RedisUtil redisUtil;
    @Value("${wechat.mp.configs.appId}")
    private String appIdp;
    @Value("${wechat.mp.configs.appSecret}")
    private String appSecretp;


    public String creatAccrssToken() {
        try {
            String appId = StringUtils.getAccessMassege(redisUtil,appIdp,"appId");
            String appSecret = StringUtils.getAccessMassege(redisUtil,appSecretp,"appSecretp");
            //String appId = "wx0e82324b8c475e52";
            //String appSecret = "88b28b0d9bb46f5476f35d597c50e939";
            String url = String.format(org.kjtc.wxapi.WxMpService.GET_ACCESS_TOKEN_URL,appId,appSecret);

            Request request = new Request.Builder().url(url).get().build();
            OkHttpClient httpClient = new OkHttpClient();
            Response response = httpClient.newCall(request).execute();
            String resultContent = response.body().string();
            WxError error = WxError.fromJson(resultContent, WxType.MP);
            if (error.getErrorCode() != 0) {
                try {
                    throw new WxErrorException(error);
                } catch (WxErrorException e) {
                    e.printStackTrace();
                }
            }
            WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
            String  token  =  accessToken.getAccessToken().replaceAll("\"","");
            //写入redis
            redisUtil.set("accessToken:temporaryKey",token);
        } catch (IOException e) {
            System.out.println("获取token失败");
        }
        return "";
    }


   public Map<String, String> getJsjdk(String url,String longitude,String latitude, String chargingstationname, String chargingstationaddress){
        //获取Token
       String accessToken =  redisUtil.get("accessToken:temporaryKey").toString();
       Map<String, String>   jssdk = JsSignUtil.sign(url,appIdp,accessToken);
       jssdk.put("longitude",longitude);
       jssdk.put("latitude",latitude);
       jssdk.put("chargingstationname",chargingstationname);
       jssdk.put("chargingstationaddress",chargingstationaddress);
       System.out.println(url);
        return jssdk;
   }


}
